//
//  LeftBarButtonItemable.swift
//  Exam-iOS
//
//  Created by 李胜锋 on 2018/3/19.
//  Copyright © 2018年 李胜锋. All rights reserved.
//

import Foundation
import RxSwift

///导航栏返回按钮
protocol LeftBarButtonItemable {
    
}

extension LeftBarButtonItemable where Self: ViewController {
    ///设置导航栏返回按钮
    func setLeftItem(_ block: VoidBlock? = nil) {
        let leftItem = getBackBarButtonItem()
        leftItem.rx.tap.bind { [weak self] in
            guard let strongSelf = self else { return }
            if block != nil {
                block?()
            }
            else {
                strongSelf.navigationController?.popViewController(animated: true)
            }
        }.disposed(by: disposeBag)
        navigationItem.leftBarButtonItem = leftItem
    }
    //导航栏返回按钮
    private func getBackBarButtonItem(tintColor:UIColor = UIColor.white) -> UIBarButtonItem {
        let leftBarItem = UIBarButtonItem.init()
        leftBarItem.tintColor = tintColor
        leftBarItem.image = UIImage.init(named: "white_back")
        leftBarItem.style = .plain
        
        return leftBarItem
    }
}


